package com.wwdx.utils.jackson;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wwdx.utils.ConstantHolder;

import java.text.SimpleDateFormat;
import java.util.TimeZone;

/**
 * 自定义jackson参数
 * @author liguoliang
 * @date 2018/4/18 13:09
 */
public class CustomObjectMapper extends ObjectMapper {
    
    public CustomObjectMapper(){
        super();
        //时间格式化
        setDateFormat(new SimpleDateFormat(ConstantHolder.Constant.LOCAL_DATE_TIME_PATTERN));
        //东八区
        setTimeZone(TimeZone.getTimeZone("GMT+8"));
        //去除为null的JSON字段
        setSerializationInclusion(JsonInclude.Include.NON_NULL);
        //去除为""的JSON字段
        //setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

        //转换为null的JSON字段为""
        /*getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>(){
            @Override
            public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
                jgen.writeString("");
            }
        });*/
    }
}